package 多态.多态的优缺点;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-06-16
 * Time: 21:39
 */
public class drawShapes {
    //例如我们现在需要打印的不是一个形状了, 而是多个形状. 如果不基于多态, 实现代码如下:
    public static void drawShapes1() {
        Rect rect = new Rect();
        Cycle cycle = new Cycle();
        Flower flower = new Flower();
        String[] shapes = {"cycle", "rect", "cycle", "rect", "flower"};

        for(String shape : shapes) {
            if(shape.equals("cycle")) {
                cycle.draw();
            }
            else if(shape.equals("rect")) {
                rect.draw();
            }else {
                flower.draw();
            }
        }
    }

    //如果使用使用多态, 则不必写这么多的 if - else 分支语句, 代码更简单。
    public static void drawShapes2() {
        //创建一个Shape对象的数组
        Shape[] shapes = {new Cycle(), new Rect(), new Cycle(), new Rect(), new Flower()};
        for(Shape shape : shapes) {
            shape.draw();
        }
    }
    public static void main(String[] args) {
        drawShapes2();
        //drawShapes1();
    }
}
